package com.ssy.lingxi.component.rabbitMQ.config;

import com.ssy.lingxi.component.rabbitMQ.constants.MessageConstants;
import org.springframework.amqp.core.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitMQ消息队列定义
 *
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/7/29
 */
@Configuration
@ConditionalOnExpression("T(org.springframework.util.StringUtils).hasLength('${spring.rabbitmq.host:}') or T(org.springframework.util.StringUtils).hasLength('${spring.rabbitmq.addresses:}')")
public class MQConfig {

    /*********************************** 会员资金账户充值 *****************************************/
    //充值队列
    @Bean
    Queue pay_recharge_queue() {
        Map<String, Object> params = new HashMap<>();
        // x-dead-letter-exchange 声明了队列里的死信转发到的DLX名称，
        params.put("x-dead-letter-exchange", MessageConstants.pay_recharge_delay_exchange);
        // x-dead-letter-routing-key 声明了这些死信在转发时携带的 routing-key 名称。
        params.put("x-dead-letter-routing-key", MessageConstants.pay_recharge_routingKey);
        return new Queue(MessageConstants.pay_recharge_queue, true, false, false, params);
    }

    @Bean
    DirectExchange pay_recharge_exchange() {
        return new DirectExchange(MessageConstants.pay_recharge_exchange, true, false);
    }

    @Bean
    Binding pay_recharge_binding() {
        return BindingBuilder.bind(pay_recharge_queue()).to(pay_recharge_exchange()).with(MessageConstants.pay_recharge_routingKey);
    }

    // 创建一个延时队列
    @Bean
    Queue pay_recharge_delay_queue() {
        return new Queue(MessageConstants.pay_recharge_delay_queue, true, false, false);
    }

    @Bean
    DirectExchange pay_recharge_delay_exchange() {
        return new DirectExchange(MessageConstants.pay_recharge_delay_exchange, true, false);
    }

    @Bean
    Binding pay_recharge_delay_binding() {
        return BindingBuilder.bind(pay_recharge_delay_queue()).to(pay_recharge_delay_exchange()).with(MessageConstants.pay_recharge_routingKey);
    }


    /*********************************** 操作通知 *****************************************/

    @Bean
    Queue operate_notice_queue() {
        return new Queue(MessageConstants.operate_notice_queue, false);
    }

    @Bean
    TopicExchange operate_notice_exchange() {
        return new TopicExchange(MessageConstants.operate_notice_exchange);
    }

    @Bean
    Binding operate_notice_binding() {
        return BindingBuilder.bind(operate_notice_queue()).to(operate_notice_exchange()).with(MessageConstants.operate_notice_routingKey);
    }

    //********************************* 招投标--报名时间截止，外部状态改成待审核报名 *****************************************

    //延时队列交换机，注意这里的交换机类型：CustomExchange
    @Bean
    public CustomExchange purchaseSubmitTenderDelayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        //设置手动应答模式，消息持久化、死信消息
        return new CustomExchange(MessageConstants.purchase_submit_tender_delay_exchange, "x-delayed-message", true, false, args);
    }

    /**
     * 延时队列
     */
    @Bean
    public Queue purchaseSubmitTenderDelayQueue() {
        return new Queue(MessageConstants.purchase_submit_tender_delay_queue, true);
    }

    /**
     * 给延时队列绑定交换机
     */
    @Bean
    public Binding purchaseSubmitTenderDelayBinding(Queue purchaseSubmitTenderDelayQueue, CustomExchange purchaseSubmitTenderDelayExchange) {
        return BindingBuilder.bind(purchaseSubmitTenderDelayQueue).to(purchaseSubmitTenderDelayExchange).with(MessageConstants.purchase_submit_tender_routingKey).noargs();
    }

    // ********************************* 招投标--提交资格预审时间开始，外部状态改成待提交资格预审 *****************************************
    //延时队列Exchange类型：CustomExchange
    @Bean
    public CustomExchange purchaseNotSubmitQualificationsCheckDelayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        //设置手动应答模式，消息持久化、死信消息
        return new CustomExchange(MessageConstants.purchase_not_submit_qualifications_check_delay_exchange, "x-delayed-message", true, false, args);
    }

    /**
     * 延时队列
     */
    @Bean
    public Queue purchaseNotSubmitQualificationsCheckDelayQueue() {
        return new Queue(MessageConstants.purchase_not_submit_qualifications_check_delay_queue, true);
    }

    /**
     * 给延时队列绑定交换机
     */
    @Bean
    public Binding purchaseNotSubmitQualificationsCheckDelayBinding(Queue purchaseNotSubmitQualificationsCheckDelayQueue, CustomExchange purchaseNotSubmitQualificationsCheckDelayExchange) {
        return BindingBuilder.bind(purchaseNotSubmitQualificationsCheckDelayQueue).to(purchaseNotSubmitQualificationsCheckDelayExchange).with(MessageConstants.purchase_not_submit_qualifications_check_routingKey).noargs();
    }

    // ********************************* 招投标--提交资格预审时间截止，外部状态改成待资格预审 *****************************************

    //延时队列Exchange类型：CustomExchange
    @Bean
    public CustomExchange purchaseNotQualificationsCheckDelayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        //设置手动应答模式，消息持久化、死信消息
        return new CustomExchange(MessageConstants.purchase_not_qualifications_check_delay_exchange, "x-delayed-message", true, false, args);
    }

    /**
     * 延时队列
     */
    @Bean
    public Queue purchaseNotQualificationsCheckDelayQueue() {
        return new Queue(MessageConstants.purchase_not_qualifications_check_delay_queue, true);
    }

    /**
     * 给延时队列绑定交换机
     */
    @Bean
    public Binding purchaseNotQualificationsCheckDelayBinding(Queue purchaseNotQualificationsCheckDelayQueue, CustomExchange purchaseNotQualificationsCheckDelayExchange) {
        return BindingBuilder.bind(purchaseNotQualificationsCheckDelayQueue).to(purchaseNotQualificationsCheckDelayExchange).with(MessageConstants.purchase_not_qualifications_check_routingKey).noargs();
    }

    // *********************************** 招投标--投标时间开始，外部状态改成待投标 *****************************************
    //延时队列Exchange类型：CustomExchange
    @Bean
    public CustomExchange purchaseNotSubmitTenderDelayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        //设置手动应答模式，消息持久化、死信消息
        return new CustomExchange(MessageConstants.purchase_not_submit_tender_delay_exchange, "x-delayed-message", true, false, args);
    }

    /**
     * 延时队列
     */
    @Bean
    public Queue purchaseNotSubmitTenderDelayQueue() {
        return new Queue(MessageConstants.purchase_not_submit_tender_delay_queue, true);
    }

    /**
     * 给延时队列绑定交换机
     */
    @Bean
    public Binding purchaseNotSubmitTenderDelayBinding(Queue purchaseNotSubmitTenderDelayQueue, CustomExchange purchaseNotSubmitTenderDelayExchange) {
        return BindingBuilder.bind(purchaseNotSubmitTenderDelayQueue).to(purchaseNotSubmitTenderDelayExchange).with(MessageConstants.purchase_not_submit_tender_routingKey).noargs();
    }

    // *********************************** 招投标--投标时间截止，外部状态改成待开标 *****************************************
    //延时队列Exchange类型：CustomExchange
    @Bean
    public CustomExchange purchaseNotOpenTenderDelayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        //设置手动应答模式，消息持久化、死信消息
        return new CustomExchange(MessageConstants.purchase_not_open_tender_delay_exchange, "x-delayed-message", true, false, args);
    }

    /**
     * 延时队列
     */
    @Bean
    public Queue purchaseNotOpenTenderDelayQueue() {
        return new Queue(MessageConstants.purchase_not_open_tender_delay_queue, true);
    }

    /**
     * 给延时队列绑定交换机
     */
    @Bean
    public Binding purchaseNotOpenTenderDelayBinding(Queue purchaseNotOpenTenderDelayQueue, CustomExchange purchaseNotOpenTenderDelayExchange) {
        return BindingBuilder.bind(purchaseNotOpenTenderDelayQueue).to(purchaseNotOpenTenderDelayExchange).with(MessageConstants.purchase_not_open_tender_routingKey).noargs();
    }

    // *********************************** 招投标--开标时间到，外部状态改成待评标 *****************************************

    //延时队列Exchange类型：CustomExchange
    @Bean
    public CustomExchange purchaseOpenTenderDelayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        //设置手动应答模式，消息持久化、死信消息
        return new CustomExchange(MessageConstants.purchase_open_tender_delay_exchange, "x-delayed-message", true, false, args);
    }

    /**
     * 延时队列
     */
    @Bean
    public Queue purchaseOpenTenderDelayQueue() {
        return new Queue(MessageConstants.purchase_open_tender_delay_queue, true);
    }

    /**
     * 给延时队列绑定交换机
     */
    @Bean
    public Binding purchaseOpenTenderDelayBinding(Queue purchaseOpenTenderDelayQueue, CustomExchange purchaseOpenTenderDelayExchange) {
        return BindingBuilder.bind(purchaseOpenTenderDelayQueue).to(purchaseOpenTenderDelayExchange).with(MessageConstants.purchase_open_tender_routingKey).noargs();
    }

    // *********************************** 招投标--评标时间截止，内部状态改成待提交评标报告 *****************************************

    //延时队列Exchange类型：CustomExchange
    @Bean
    public CustomExchange purchaseEvaluationTenderDelayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        //设置手动应答模式，消息持久化、死信消息
        return new CustomExchange(MessageConstants.purchase_evaluation_tender_delay_exchange, "x-delayed-message", true, false, args);
    }

    /**
     * 延时队列
     */
    @Bean
    public Queue purchaseEvaluationTenderDelayQueue() {
        return new Queue(MessageConstants.purchase_evaluation_tender_delay_queue, true);
    }

    /**
     * 给延时队列绑定交换机
     */
    @Bean
    public Binding purchaseEvaluationTenderDelayBinding(Queue purchaseEvaluationTenderDelayQueue, CustomExchange purchaseEvaluationTenderDelayExchange) {
        return BindingBuilder.bind(purchaseEvaluationTenderDelayQueue).to(purchaseEvaluationTenderDelayExchange).with(MessageConstants.purchase_evaluation_tender_routingKey).noargs();
    }

    // ******************************************************采购竞价**********************************************************************

    //延时队列Exchange类型：CustomExchange
    @Bean
    public CustomExchange delayPurchaseExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        //设置手动应答模式，消息持久化、死信消息
        return new CustomExchange(MessageConstants.PURCHASE_BIDDING_DELAY_EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 延时队列
     */
    @Bean
    public Queue delayPurchaseQueue() {
        return new Queue(MessageConstants.PURCHASE_BIDDING_DELAY_QUEUE, true);
    }

    /**
     * 给延时队列绑定交换机
     */
    @Bean
    public Binding cfgPurchaseDelayBinding(Queue delayPurchaseQueue, CustomExchange delayPurchaseExchange) {
        return BindingBuilder.bind(delayPurchaseQueue).to(delayPurchaseExchange).with(MessageConstants.PURCHASE_BIDDING_DELAY_KEY).noargs();
    }

    /**
     * 延时队列交换机
     * 注意这里的交换机类型：CustomExchange
     */
    @Bean
    public CustomExchange delayEndPurchaseExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        //设置手动应答模式，消息持久化、死信消息
        return new CustomExchange(MessageConstants.PURCHASE_END_BIDDING_DELAY_EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 延时队列
     */
    @Bean
    public Queue delayEndPurchaseQueue() {
        return new Queue(MessageConstants.PURCHASE_END_BIDDING_DELAY_QUEUE, true);
    }

    /**
     * 给延时队列绑定交换机
     */
    @Bean
    public Binding cfgPurchaseDelayEndBinding(Queue delayEndPurchaseQueue, CustomExchange delayEndPurchaseExchange) {
        return BindingBuilder.bind(delayEndPurchaseQueue).to(delayEndPurchaseExchange).with(MessageConstants.PURCHASE_END_BIDDING_DELAY_KEY).noargs();
    }




    // ******************************************************采购询价**********************************************************************
    //延时队列Exchange类型：CustomExchange
    @Bean
    public CustomExchange delayPurchaseInquiryExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        //设置手动应答模式，消息持久化、死信消息
        return new CustomExchange(MessageConstants.PURCHASE_INQUIRY_DELAY_EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 延时队列
     */
    @Bean
    public Queue delayPurchaseInquiryQueue() {
        return new Queue(MessageConstants.PURCHASE_INQUIRY_DELAY_QUEUE, true);
    }

    /**
     * 给延时队列绑定交换机
     */
    @Bean
    public Binding cfgPurchaseDelayInquiry(Queue delayPurchaseInquiryQueue, CustomExchange delayPurchaseInquiryExchange) {
        return BindingBuilder.bind(delayPurchaseInquiryQueue).to(delayPurchaseInquiryExchange).with(MessageConstants.PURCHASE_INQUIRY_DELAY_KEY).noargs();
    }

    /*###################################################### OPEN API ####################################################*/

    /**
     * 商品信息
     */
    @Bean
    Queue open_api_commodity_queue() {
        return new Queue(MessageConstants.OPEN_API_COMMODITY_QUEUE, false);
    }

    @Bean
    DirectExchange open_api_commodity_exchange() {
        return new DirectExchange(MessageConstants.OPEN_API_COMMODITY_EXCHANGE);
    }

    @Bean
    Binding open_api_commodity_binding() {
        return BindingBuilder.bind(open_api_commodity_queue()).to(open_api_commodity_exchange()).with(MessageConstants.OPEN_API_COMMODITY_ROUTINGKEY);
    }

    /**
     * 品牌信息
     */
    @Bean
    Queue open_api_brand_queue() {
        return new Queue(MessageConstants.OPEN_API_BRAND_QUEUE, false);
    }

    @Bean
    DirectExchange open_api_brand_exchange() {
        return new DirectExchange(MessageConstants.OPEN_API_BRAND_EXCHANGE);
    }

    @Bean
    Binding open_api_brand_binding() {
        return BindingBuilder.bind(open_api_brand_queue()).to(open_api_brand_exchange()).with(MessageConstants.OPEN_API_BRAND_ROUTINGKEY);
    }

    // *********************************** 平台后台服务 *****************************************

    /**
     * 单位信息
     */
    @Bean
    Queue open_api_unit_queue() {
        return new Queue(MessageConstants.OPEN_API_UNIT_QUEUE, false);
    }

    @Bean
    DirectExchange open_api_unit_exchange() {
        return new DirectExchange(MessageConstants.OPEN_API_UNIT_EXCHANGE);
    }

    @Bean
    Binding open_api_unit_binding() {
        return BindingBuilder.bind(open_api_unit_queue()).to(open_api_unit_exchange()).with(MessageConstants.OPEN_API_UNIT_ROUTINGKEY);
    }

    /**
     * 行政区域信息
     */
    @Bean
    Queue open_api_area_queue() {
        return new Queue(MessageConstants.OPEN_API_AREA_QUEUE, false);
    }

    @Bean
    DirectExchange open_api_area_exchange() {
        return new DirectExchange(MessageConstants.OPEN_API_AREA_EXCHANGE);
    }

    @Bean
    Binding open_api_area_binding() {
        return BindingBuilder.bind(open_api_area_queue()).to(open_api_area_exchange()).with(MessageConstants.OPEN_API_AREA_ROUTINGKEY);
    }


    // *********************************** 售后服务 *****************************************

    /**
     * 售后-退货信息
     */
    @Bean
    Queue open_api_after_sale_queue() {
        return new Queue(MessageConstants.OPEN_API_AS_RETURN_QUEUE, false);
    }

    @Bean
    DirectExchange open_api_after_sale_exchange() {
        return new DirectExchange(MessageConstants.OPEN_API_AS_RETURN_EXCHANGE);
    }

    @Bean
    Binding open_api_after_sale_binding() {
        return BindingBuilder.bind(open_api_after_sale_queue()).to(open_api_after_sale_exchange()).with(MessageConstants.OPEN_API_AS_RETURN_ROUTINGKEY);
    }




}
